<?php
session_start();
require_once "values.php";

if ($user=="")
{
require_once "database.php";



$result = mysql_query("SELECT * FROM tblUsers where username = '$_POST[username]' ");
$rows = mysql_fetch_array($result);
$user=$rows[Name];


if($rows[Username]=="")
{
echo 'Usrename or Password Wrong please <a href="../index.php">retry</a>';
echo '<p>'.$rows[Username];
}
else 
{

if ($ldap=='false')
{


$password = $_POST[password];
$password = md5($password);

if ($password == $rows[Password])
{


$_SESSION["user"]=$user;

header("Location: ../index.php");


}
else
{
echo 'Usrename or Password Wrong please <a href="../index.php">retry</a>';

}
}
else
{
  #Ldap auth

 
  $ad = ldap_connect($ldapaddress)
          or die("Couldn't connect to LDAP Fatal Server Error !");

    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
  
    $bd = ldap_bind($ad,$_POST[username].'@'.$domain,$_POST[password])
          or die('LDAP Usrename or Password Wrong please <a href="../index.php">retry</a>');

if($bd==true)
{

$_SESSION["user"]=$user;
header("Location: ../index.php");

}

}
}

}




?>
